package ua.com.mind_tree.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.*;
import ua.com.mind_tree.client.experience.Experience;
import ua.com.mind_tree.client.experience.ExperienceType;
import ua.com.mind_tree.client.experience.Tag;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author suamo ( 01.05.12 17:17 )
 */
public class ExpDetailsPanel extends VerticalPanel {
    private TextBox nameBox = new TextBox();
    private TextBox creatorBox = new TextBox();
    private ListBox typeBox = new ListBox();
    private ListBox tagListBox = new ListBox();
    private TextBox parentObject = new TextBox();
    private TextBox dateBox = new TextBox();

    Button saveButton = new Button("Сохранить");
    Button saveAsNewButton = new Button("Сохранить как новый");

    public ExpDetailsPanel() {
        addRow("Название: ", nameBox);
        addRow("Создатель: ", creatorBox);

        for (ExperienceType type : ExperienceType.values()){
            typeBox.addItem(type.name());
        }

        addRow("Тип: ", typeBox);
        addRow("Тэги: ", tagListBox);
        addRow("Изучен после: ", parentObject);
        addRow("Дата: ", dateBox);
        saveButton.setEnabled(false);

        HorizontalPanel buttonPanel = new HorizontalPanel();
        buttonPanel.addStyleName("buttonPanel");
        buttonPanel.add(saveButton);

        saveAsNewButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                Experience experience = getConstructedExperience();
                Main.TEST_DATA.add(experience);
                Main.provider.getList().add(experience);
            }
        });
        buttonPanel.add(saveAsNewButton);
        add(buttonPanel);
    }

    private void addRow(String name, Widget widget) {
        HorizontalPanel panel = new HorizontalPanel();
        Label label = new Label(name);
        label.addStyleName("detailsLabel");
        panel.add(label);
        panel.add(widget);
        add(panel);
    }

    public void update(Experience experience) {
        nameBox.setValue(experience.getName());
        creatorBox.setValue(experience.getCreator());

        tagListBox.clear();
        for (Tag tag : experience.getTags()) {
            tagListBox.addItem(tag.getName());
        }
        tagListBox.setVisibleItemCount(experience.getTags().size());

        typeBox.setSelectedIndex(experience.getType().equals(ExperienceType.BOOK)?0:1);

        parentObject.setValue(experience.getParent().getName());
        dateBox.setValue(experience.getAddingDate().toString());
        saveButton.setEnabled(true);
    }

    public Experience getConstructedExperience() {
        Experience exp = new Experience();
        exp.setName(nameBox.getValue());
        exp.setCreator(creatorBox.getValue());
        exp.setAddingDate(new Date());
        exp.setParent(new Experience());
        exp.setTags(getTagsAsList());
        exp.setType(ExperienceType.valueOf(typeBox.getValue(typeBox.getSelectedIndex())));
        return exp;
    }

    public List<Tag> getTagsAsList() {
        List<Tag> tags = new ArrayList<Tag>();
        for (int i = 0; i < tagListBox.getItemCount(); i++){
            String value = tagListBox.getValue(i);
            tags.add(new Tag(value));
        }

        return tags;
    }
}
